package com.mobidev.webview; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; /** * Created by lawrence on 7/10/15. */ public class MyBrowserActivity extends AppCompatActivity { public static final String ARG_URL = "my_url"; private String urlToLoad; private WebView webView; private Bundle extras; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_browser); getSupportActionBar().setDisplayHomeAsUpEnabled(true); extras = getIntent().getExtras(); if (extras != null) { urlToLoad = extras.getString(ARG_URL); } progressBar = (ProgressBar) findViewById(R.id.progressbar); webView = (WebView) findViewById(R.id.webview); webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setJavaScriptEnabled(true); webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); webView.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); // Show progressbar progressBar.setVisibility(View.VISIBLE); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Show error // Stop spinner or progressbar progressBar.setVisibility(View.GONE); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); // Stop spinner or progressBar progressBar.setVisibility(View.GONE); } }); webView.setWebChromeClient(new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { // Activities and WebViews measure progress with different scales. // The progress meter will automatically disappear when we reach 100% progressBar.setProgress(progress * 100); } }); webView.loadUrl(urlToLoad); } }